/**
 * Created by a1 on 2017/11/1.
 */
"use strict";
var fs = require('fs');
var formidable = require('formidable');
var common = require('../models/common.js');
var BookModel = require('../models/Book.js');//书本Schema
var HotsearchModel = require('../models/Hotsearch.js');//书本Schema

exports.search=function(req,res){
    res.render('search',{
        page:'search',
        title:req.query.kw
    });
};
exports.getSearchBook=function(req,res){
    var form=new formidable.IncomingForm();
    form.parse(req,function(err,fields,files){
        if(err){
            res.json({"msg":'失败',"code":0,info:{}});
            return;
        }
        let kw=fields.kw;
        let tag=parseInt(fields.tag);
        let page=parseInt(fields.page);
        let pagenum=page*10;
        if(kw==""){
            res.json({"msg":'成功',"code":1,info:{}});
        }else{
            let re = new RegExp(kw);
            switch(tag){
                case 1:
                    BookModel.findLimitSkip({$or:[{"bName":re},{"desc":re}]},10,pagenum,{subscribe:-1},function(err,docs){
                        if(err){
                            res.json({"msg":'失败',"code":0,info:{}});
                        }
                        BookModel.findConut({$or:[{"bName":re},{"desc":re}]},function(err,count){
                            if(err){
                                res.json({"msg":'失败',"code":0,info:{}});
                            }
                            res.json({"msg":'成功',"code":1,info:docs,length:count});
                        });
                    });
                    break;
                case 2:
                    BookModel.findLimitSkip({$or:[{"bName":re},{"desc":re}]},10,pagenum,{updateTime:-1},function(err,docs){
                        if(err){
                            res.json({"msg":'失败',"code":0,info:{}});
                        }
                        BookModel.findConut({$or:[{"bName":re},{"desc":re}]},function(err,count){
                            if(err){
                                res.json({"msg":'失败',"code":0,info:{}});
                            }
                            res.json({"msg":'成功',"code":1,info:docs,length:count});
                        });
                    });
                    break;
                case 3:
                    BookModel.findLimitSkip({$or:[{"bName":re},{"desc":re}]},10,pagenum,{cnt:-1},function(err,docs){
                        if(err){
                            res.json({"msg":'失败',"code":0,info:{}});
                        }
                        BookModel.findConut({$or:[{"bName":re},{"desc":re}]},function(err,count){
                            if(err){
                                res.json({"msg":'失败',"code":0,info:{}});
                            }
                            res.json({"msg":'成功',"code":1,info:docs,length:count});
                        });
                    });
                    break;
                case 4:
                    BookModel.findLimitSkip({$or:[{"bName":re},{"desc":re}]},10,pagenum,{recomendCnt:-1},function(err,docs){
                        if(err){
                            res.json({"msg":'失败',"code":0,info:{}});
                        }
                        BookModel.findConut({$or:[{"bName":re},{"desc":re}]},function(err,count){
                            if(err){
                                res.json({"msg":'失败',"code":0,info:{}});
                            }
                            res.json({"msg":'成功',"code":1,info:docs,length:count});
                        });
                    });
                    break;
                case 5:
                    BookModel.findLimitSkip({$or:[{"bName":re},{"desc":re}]},10,pagenum,{recomm:-1},function(err,docs){
                        if(err){
                            res.json({"msg":'失败',"code":0,info:{}});
                        }
                        BookModel.findConut({$or:[{"bName":re},{"desc":re}]},function(err,count){
                            if(err){
                                res.json({"msg":'失败',"code":0,info:{}});
                            }
                            res.json({"msg":'成功',"code":1,info:docs,length:count});
                        });
                    });
                    break;
                case 6:
                    BookModel.findLimitSkip({$or:[{"bName":re},{"desc":re}]},10,pagenum,{clickCnt:-1},function(err,docs){
                        if(err){
                            res.json({"msg":'失败',"code":0,info:{}});
                        }
                        BookModel.findConut({$or:[{"bName":re},{"desc":re}]},function(err,count){
                            if(err){
                                res.json({"msg":'失败',"code":0,info:{}});
                            }
                            res.json({"msg":'成功',"code":1,info:docs,length:count});
                        });
                    });
                    break;
                default:
                    break;
            }
        }
    });
};
exports.getSearchBookKeyUp=function(req,res){
    var form=new formidable.IncomingForm();
    form.parse(req,function(err,fields,files){
        if(err){
            res.json({"msg":'失败',"code":0,info:{}});
            return;
        }
        let kw=fields.kw;
        let tag=parseInt(fields.tag);
        let page=parseInt(fields.page);
        let pagenum=page*10+10;
        if(kw==""){
            res.json({"msg":'成功',"code":1,info:{}});
        }else{
            let re = new RegExp(kw);
            switch(tag){
                case 1:
                    BookModel.findLimitSkip({$or:[{"bName":re}]},pagenum,page*10,{subscribe:-1},function(err,docs){
                        if(err){
                            res.json({"msg":'失败',"code":0,info:{}});
                        }
                        res.json({"msg":'成功',"code":1,info:docs});
                    });
                    break;
                default:
                    break;
            }
        }
    });
};
exports.setHotsearch=function(req,res){
    var form=new formidable.IncomingForm();
    form.parse(req,function(err,fields,files) {
        if (err) {
            res.json({"msg": '失败', "code": 0, info: {}});
            return;
        }
        let kw = fields.kw;
        HotsearchModel.findF({},function(err,docs){
            if(docs==null||docs==""){
                let obj={
                    hsid:1,
                    bName:kw,
                    num:1
                };
                let data=new HotsearchModel(obj);
                data.save(function(err){
                    res.json({"msg":'成功',"code":1});
                });
            }else{
                HotsearchModel.findSortOne({bName:kw},{hsid:1},function(err,doc){
                    if(doc==null||doc==""){
                        HotsearchModel.findSortOne({},{hsid:-1},function(err,docs1){
                            let obj={
                                hsid:parseInt(docs1.hsid)+1,
                                bName:kw,
                                num:1
                            };
                            let data=new HotsearchModel(obj);
                            data.save(function(err){
                                res.json({"msg":'成功',"code":1});
                            });
                        });
                    }else{
                        HotsearchModel.updateFn({bName:kw},{$inc:{num:1}},function(err,docs2){
                            res.json({"msg":'成功',"code":1});
                        });
                    }
                });
            }
        });
    });
};
exports.allSearch=function(req,res){
    HotsearchModel.findFnLimit({},{num:-1},10,function(err,docs){
        if (err) {
            res.json({"msg": '失败', "code": 0, info: {}});
            return;
        }
        res.json({
            code:1,
            bookInfo:docs,
            msg:"获取热门搜索成功"
        });
    });
};